<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属城市" prop="cityId">
        <el-input
          v-model="queryParams.cityId"
          placeholder="请输入所属城市"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纬度坐标" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入纬度坐标"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度坐标" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入经度坐标"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['managent:cover:add']"
        >新增</el-button>
      </el-col>

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coverList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="所属城市" align="center" prop="cityId" >
        <template #default="scope">
        <el-tag>
          {{ filteredCityName(scope.row.cityId) }}
        </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="纬度坐标" align="center" prop="latitude" />
      <el-table-column label="经度坐标" align="center" prop="longitude" />
      <el-table-column label="状态" align="center" prop="coverStatus" />
      <el-table-column label="创建日期" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人ID" align="center" prop="createBy" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['managent:cover:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['managent:cover:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改井盖档案对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="coverRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属城市" prop="cityId">
          <el-select v-model="form.cityId" placeholder="请选择城市" @change="handleCityChange">
            <el-option
            v-for="city in cityList"
            :key="city.id"
            :label="city.name"
            :value="city.id"
          ></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="图片地址" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        
        <el-form-item label="地图" prop="map">
          <index style="width: 100%; height: 300px;" @map-click="handleMapClick"/>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cover">
import { listCover, getCover, delCover, addCover, updateCover } from "@/api/managent/cover";
import { listCities, getCities, delCities, addCities, updateCities } from "@/api/city/cities";
import index from "@/components/Map/index.vue";
import { reactive } from "vue";

const { proxy } = getCurrentInstance();
const { city} = proxy.useDict('city');

const coverList = ref([]);
const cityList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const filteredCityName = (cityId) => {
  const city = cityList.value.find(city => city.stateCode == cityId);
  return city ? city.name : '';
};

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    cityId: null,
    image: null,
    latitude: null,
    longitude: null,
    coverStatus: null,
  },
  rules: {
    cityId: [
      { required: true, message: "所属城市不能为空", trigger: "blur" }
    ],
    image: [
      { required: true, message: "图片地址不能为空", trigger: "blur" }
    ],
    latitude: [
      { required: true, message: "纬度坐标不能为空", trigger: "blur" }
    ],
    longitude: [
      { required: true, message: "经度坐标不能为空", trigger: "blur" }
    ],
    coverStatus: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    createBy: [
      { required: true, message: "创建人ID不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新日期不能为空", trigger: "blur" }
    ],
    updateBy: [
      { required: true, message: "更新人ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);


function handleMapClick({lng, lat}){
  form.value.latitude = lat;
  form.value.longitude = lng;
}

/** 查询井盖档案列表 */
function getList() {
  loading.value = true;
  listCover(queryParams.value).then(response => {
    coverList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleCityChange(cityId) {
  const selectedCity = cityList.value.find(city => city.stateCode === cityId);
  if (selectedCity) {
    proxy.$emit('update-map-center', { lng: selectedCity.longitude, lat: selectedCity.latitude });
  }
}

function getCityList() {
  listCities().then(response => {
    cityList.value = response.rows;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    cityId: null,
    image: null,
    latitude: null,
    longitude: null,
    coverStatus: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    deleteTime: null,
    deleteBy: null
  };
  proxy.resetForm("coverRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加井盖档案";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCover(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改井盖档案";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["coverRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCover(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCover(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除井盖档案编号为"' + _ids + '"的数据项？').then(function() {
    return delCover(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('managent/cover/export', {
    ...queryParams.value
  }, `cover_${new Date().getTime()}.xlsx`)
}

getList();
getCityList();
</script>
